{smcl}
{com}{sf}{ul off}{txt}{.-}
      name:  {res}<unnamed>
       {txt}log:  {res}C:\Users\thies\OneDrive\00_Promotion\00_Output\00_Paper\2022_Predicting_Econ_Sanctions\Empirics\20231214_Replication\Log_files/01c_Onset_limited_sample_EU.smcl
  {txt}log type:  {res}smcl
 {txt}opened on:  {res}18 Dec 2023, 16:17:21
{txt}
{com}. 
. ***************************************************************
. ***EU***
. ***************************************************************
. 
. set seed 1234
{txt}
{com}. 
. *Prepare data
. use "Dataset.dta", clear
{txt}
{com}. keep if sender=="EU"
{txt}(10,154 observations deleted)

{com}. 
. gen ln_oil_gas_value_2014 = ln(oil_gas_value_2014+1)
{txt}(681 missing values generated)

{com}. gen sender_colony=0
{txt}
{com}. replace sender_colony=1 if ht_colonial==2 | ht_colonial==3 | ht_colonial==6 | ht_colonial==7 | ht_colonial==8 | ht_colonial==9 | ht_colonial==10
{txt}(3,047 real changes made)

{com}. 
. gen sender_additional=cond(threatUS==1 | impositionUS == 1, 1, 0)
{txt}
{com}. gen only_threat=cond(threatEU==1 & impositionEU == 0, 1, 0)
{txt}
{com}. 
. gen sender_trade = ln_EU_Trade_Eurostat
{txt}(66 missing values generated)

{com}. gen coup_dummy = coup1
{txt}(5 missing values generated)

{com}. replace coup_dummy = 0 if coup_dummy == 1
{txt}(61 real changes made)

{com}. replace coup_dummy = 1 if coup_dummy == 2
{txt}(45 real changes made)

{com}. 
. * Dependent variable : 1 if a threat or sanction case started in the dyad
. xtset ccodecow year
{res}
{col 1}{txt:Panel variable: }{res:ccodecow}{txt: (unbalanced)}
{p 1 16 2}{txt:Time variable: }{res:year}{txt:, }{res:{bind:1989}}{txt: to }{res:{bind:2015}}{p_end}
{txt}{col 10}Delta: {res}1 unit
{txt}
{com}. replace caseid=0 if caseid==.
{txt}(4,590 real changes made)

{com}. gen sanctiononset = (caseid-l.caseid)
{txt}(199 missing values generated)

{com}. replace sanctiononset=1 if sanctiononset > 1 & !missing(sanctiononset)
{txt}(89 real changes made)

{com}. replace sanctiononset=0 if sanctiononset < 0
{txt}(69 real changes made)

{com}. replace sanctiononset=. if sanctiononset == 0 & (sanction_dyad == 1 | threat_dyad == 1)
{txt}(388 real changes made, 388 to missing)

{com}. tab sanctiononset

{txt}sanctionons {c |}
         et {c |}      Freq.     Percent        Cum.
{hline 12}{c +}{hline 35}
          0 {c |}{res}      4,401       98.02       98.02
{txt}          1 {c |}{res}         89        1.98      100.00
{txt}{hline 12}{c +}{hline 35}
      Total {c |}{res}      4,490      100.00
{txt}
{com}. gen sanction_test = sanctiononset if year > 2009
{txt}(4,007 missing values generated)

{com}. 
. * lag time-series variables
. sort ccodecow year
{txt}
{com}. by ccodecow: gen l_v2x_polyarchy = v2x_polyarchy[_n-1] if year==year[_n-1]+1
{txt}(803 missing values generated)

{com}. by ccodecow: gen l_gd_ptss = gd_ptss[_n-1] if year==year[_n-1]+1
{txt}(579 missing values generated)

{com}. by ccodecow: gen l_coup_dummy = coup_dummy[_n-1] if year==year[_n-1]+1
{txt}(203 missing values generated)

{com}. by ccodecow: gen l_one_sided_violence = one_sided_violence[_n-1] if year==year[_n-1]+1
{txt}(199 missing values generated)

{com}. by ccodecow: gen l_conflict = conflict[_n-1] if year==year[_n-1]+1
{txt}(199 missing values generated)

{com}. by ccodecow: gen l_mid_terr_integrity = mid_terr_integrity[_n-1] if year==year[_n-1]+1
{txt}(199 missing values generated)

{com}. by ccodecow: gen l_ln_GDPpc_imputed = ln_GDPpc_imputed[_n-1] if year==year[_n-1]+1
{txt}(318 missing values generated)

{com}. by ccodecow: gen l_sender_trade = sender_trade[_n-1] if year==year[_n-1]+1
{txt}(262 missing values generated)

{com}. by ccodecow: gen l_ln_oil_gas_value = ln_oil_gas_value_2014[_n-1] if year==year[_n-1]+1
{txt}(686 missing values generated)

{com}. by ccodecow: gen l_defense_alliance = defense_alliance[_n-1] if year==year[_n-1]+1
{txt}(199 missing values generated)

{com}. 
. * create dummy variables
. tabulate l_gd_ptss, generate (pol_terr)

  {txt}l_gd_ptss {c |}      Freq.     Percent        Cum.
{hline 12}{c +}{hline 35}
          1 {c |}{res}      1,130       25.12       25.12
{txt}          2 {c |}{res}      1,263       28.08       53.20
{txt}          3 {c |}{res}      1,212       26.95       80.15
{txt}          4 {c |}{res}        635       14.12       94.26
{txt}          5 {c |}{res}        258        5.74      100.00
{txt}{hline 12}{c +}{hline 35}
      Total {c |}{res}      4,498      100.00
{txt}
{com}. 
. 
. ** Filter for cases of importance
. keep if pot_sanctioned_countries == 1 & !missing(sanctiononset)
{txt}(1,827 observations deleted)

{com}. 
. * sortieren nach Jahr, zur Vorbereitung RF model
. gen u=0
{txt}
{com}. replace u=1 if year >= 2009
{txt}(886 real changes made)

{com}. sort u
{txt}
{com}. 
. ** Imposition
. * Random Forest Model
. rforest sanctiononset l_v2x_polyarchy pol_terr* l_coup_dummy ///
> l_one_sided_violence l_conflict l_mid_terr_integrity ///
> l_ln_GDPpc_imputed l_sender_trade l_ln_oil_gas_value ///
> sender_colony l_defense_alliance in 1/2364, type(class) iter(1500) numvars(15)
{txt}
{com}. 
. *Variable Importance
. ereturn list

{txt}scalars:
       e(Observations) =  {res}2364
           {txt}e(features) =  {res}15
         {txt}e(Iterations) =  {res}1500
          {txt}e(OOB_Error) =  {res}.0308798646362098

{txt}macros:
                e(cmd) : "{res}rforest{txt}"
            e(predict) : "{res}randomforest_predict{txt}"
             e(depvar) : "{res}sanctiononset{txt}"
         e(model_type) : "{res}random forest classification{txt}"

matrices:
         e(importance) : {res} 15 x 1
{txt}
{com}. matrix list e(importance)
{res}
{txt}e(importance)[15,1]
              Variable I~e
l_v2x_poly~y {res}    .89741123
{txt}   pol_terr1 {res}    .20537557
{txt}   pol_terr2 {res}    .60450393
{txt}   pol_terr3 {res}     .8542237
{txt}   pol_terr4 {res}    .83329259
{txt}   pol_terr5 {res}    .69012986
{txt}l_coup_dummy {res}     .2343883
{txt}l_one_side~e {res}    .59364822
{txt}  l_conflict {res}    .47341769
{txt}l_mid_terr~y {res}            1
{txt}l_ln_GDPpc~d {res}    .68259496
{txt}l_sender_t~e {res}    .62972957
{txt}l_ln_oil_g~e {res}    .50218963
{txt}sender_col~y {res}    .45440631
{txt}l_defense_~e {res}    .34245831
{reset}
{com}. * write Variable importance to excel file
. putexcel set "Supplemental_Material\Variable_Importance\Variable_Importance_Onset_EU_RF.xlsx", sheet("M") replace
{res}{txt}Note: File will be replaced when the first {cmd:putexcel} command is issued.

{com}. putexcel A1=matrix(e(importance)), names
{res}{txt}file {bf:Supplemental_Material\Variable_Importance\Variable_Importance_Onset_EU_RF.xlsx} saved

{com}. 
. * Evaluation of model performance
. ** predictions
. predict randonsEU
{txt}
{com}. predict randonsEU0 randonsEU1, pr
{txt}
{com}. 
. * Confusion Matrix
. ** Sensitivity 100, Specificity 91.4
. tab2xl sanction_test randonsEU using Supplemental_Material\Prediction_Output\Confusion_Matrixes\EU_Onset_RF, row(1) col(1)
{res}{txt}file {bf:Supplemental_Material\Prediction_Output\Confusion_Matrixes\EU_Onset_RF.xlsx} saved

{com}. diagtest sanction_test randonsEU

{txt}sanction_t {c |}   predicted classes
       est {c |}         0          1 {c |}     Total
{hline 11}{c +}{hline 22}{c +}{hline 10}
         0 {c |}{res}       742          0 {txt}{c |}{res}       742 
{txt}         1 {c |}{res}        14          2 {txt}{c |}{res}        16 
{txt}{hline 11}{c +}{hline 22}{c +}{hline 10}
     Total {c |}{res}       756          2 {txt}{c |}{res}       758 

{txt}True D defined as randonsEU ~= 0                      [95% Conf. Inter.]
-------------------------------------------------------------------------
Sensitivity                     Pr( +| D)  {res}100.00%     100.00%  100.00%
{txt}Specificity                     Pr( -|~D)  {res}98.15%      97.19%   99.11%
{txt}Positive predictive value       Pr( D| +)  {res}12.50%      10.15%   14.85%
{txt}Negative predictive value       Pr(~D| -)  {res}100.00%     100.00%  100.00%
{txt}-------------------------------------------------------------------------
Prevalence                      Pr(D)      {res} 0.26%      -0.10%    0.63%
{txt}-------------------------------------------------------------------------

{com}. 
. * AUPR .10
. prtab sanction_test randonsEU1, l2title("RF Onset", box bexpand) 

{txt}{col 12}Number of observations       =  {res}758
{txt}{col 12}Unique values of classifier  =  {res}619
{txt}{col 12}Number of positive cases     =  {res}16
{txt}{col 12}Portion of positive cases    ={res}  0.0211

{txt}{hline 50}
{col 5} Recall =  0.1250{col 25}  0.2500{col 37}  0.3125
{hline 50}
{res}{col 2}Precision{col 14}  1.0000{col 25}  0.1111{col 37}  0.0549
{txt}{hline 50}
{res}
{txt}{col 2}Area under precision-recall curve:  0.0923

{com}. graph save "Graph" "Supplemental_Material\Prediction_Output\Roc-curves\AUPR_Onset_RF_EU.gph", replace
{txt}{p 0 4 2}
(file {bf}
Supplemental_Material\Prediction_Output\Roc-curves\AUPR_Onset_RF_EU.gph{rm}
not found)
{p_end}
{res}{txt}file {bf:Supplemental_Material\Prediction_Output\Roc-curves\AUPR_Onset_RF_EU.gph} saved

{com}. 
. * .21
. kap sanction_test randonsEU

{txt}{col 14}Expected
Agreement   agreement     Kappa   Std. err.         Z      Prob>Z
{hline 65}
{res}  98.15%      97.64%     0.2186     0.0227       9.64      0.0000
{txt}
{com}. 
. log close
      {txt}name:  {res}<unnamed>
       {txt}log:  {res}C:\Users\thies\OneDrive\00_Promotion\00_Output\00_Paper\2022_Predicting_Econ_Sanctions\Empirics\20231214_Replication\Log_files/01c_Onset_limited_sample_EU.smcl
  {txt}log type:  {res}smcl
 {txt}closed on:  {res}18 Dec 2023, 16:17:42
{txt}{.-}
{smcl}
{txt}{sf}{ul off}